package com.topnav.mc.schedule.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.js.lib.utils.DateUtils;
import com.topnav.mc.admin.dao.mapper.*;
import com.topnav.mc.admin.entity.*;
import com.topnav.mc.admin.service.*;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Date;

/**
 * 清理过期数据
 * @Author: kenny
 * @Date: 2024/4/1 19:39
 */
@Slf4j
@Component
public class ClearExpiredDataJob implements Job, Serializable {
    @Autowired
    private OperateLogMapper operateLogMapper;
    @Autowired
    private IOperateLogService operateLogService;
    @Autowired
    private DeviceLogMapper deviceLogMapper;
    @Autowired
    private IDeviceLogService deviceLogService;
    @Autowired
    private DeviceOnlineTotalMapper deviceOnlineTotalMapper;
    @Autowired
    private IDeviceOnlineTotalService deviceOnlineTotalService;
    @Autowired
    private DeviceAlarmMapper deviceAlarmMapper;
    @Autowired
    private IDeviceAlarmService deviceAlarmService;
    @Autowired
    private DevicePositionMapper devicePositionMapper;
    @Autowired
    private IDevicePositionService devicePositionService;

    @Value("${expired-data.opereate-log:100000}")
    private Integer operateLogMax;
    @Value("${expired-data.device-log:1000000}")
    private Integer deviceLogMax;
    @Value("${expired-data.device-online-total:100000}")
    private Integer deviceOnlineTotalMax;
    @Value("${expired-data.device-alarm:100000}")
    private Integer deviceAlarmMax;
    @Value("${expired-data.device-position:2000000}")
    private Integer devicePositionMax;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("****** Clear Expired Data ...... ");
        //1、操作日志
        new Thread(()->{
            int count=operateLogMax+1;
            //每个循环删除一天的数据，只要多于最大值，就一直删
            while(count > operateLogMax){
                //总数
                LambdaQueryWrapper<OperateLog> operateLogWrapper = new LambdaQueryWrapper<>();
                count = operateLogMapper.selectCount(operateLogWrapper);
                log.info("****** OperateLog Count: {}",count);
                if (count <= operateLogMax){return;}
                // 取得第一条的时间，再把当天的数据删除
                operateLogWrapper.clear();
                operateLogWrapper.orderByAsc(OperateLog::getId);
                OperateLog operateLog = operateLogService.getOne(operateLogWrapper,false);
                if (operateLog != null){
                    String s = DateUtils.format(operateLog.getTime(),"yyyy-MM-dd");
                    Date from = DateUtils.parse(s + " 00:00:00","yyyy-MM-dd HH:mm:ss");
                    from  = new Date((from.getTime() + 24 * 60 * 60 * 1000L));
                    operateLogWrapper.clear();
                    operateLogWrapper.lt(OperateLog::getTime,from);
                    operateLogService.remove(operateLogWrapper);
                }
            }

        }).start();

        //2、设备日志
        new Thread(()->{
            int count=deviceLogMax+1;
            //每个循环删除一天的数据，只要多于最大值，就一直删
            while(count > deviceLogMax){
                //总数
                LambdaQueryWrapper<DeviceLog> deviceLogWrapper = new LambdaQueryWrapper<>();
                count = deviceLogMapper.selectCount(deviceLogWrapper);
                log.info("****** DeviceLog Count: {}",count);
                if (count <= deviceLogMax){
                    return;
                }
                // 取得第一条的时间，再把当天的数据删除
                deviceLogWrapper.clear();
                deviceLogWrapper.orderByAsc(DeviceLog::getId);
                DeviceLog deviceLog = deviceLogService.getOne(deviceLogWrapper,false);
                if (deviceLog != null){
                    String s = DateUtils.format(deviceLog.getCreateTime(),"yyyy-MM-dd");
                    Date from = DateUtils.parse(s + " 00:00:00","yyyy-MM-dd HH:mm:ss");
                    from  = new Date((from.getTime() + 24 * 60 * 60 * 1000L));
                    deviceLogWrapper.clear();
                    deviceLogWrapper.lt(DeviceLog::getCreateTime,from);
                    deviceLogService.remove(deviceLogWrapper);
                }
            }
        }).start();

        //3、设备在线统计
        new Thread(()->{
            int count=deviceOnlineTotalMax+1;
            //每个循环删除一天的数据，只要多于最大值，就一直删
            while(count > deviceOnlineTotalMax){
                //总数
                LambdaQueryWrapper<DeviceOnlineTotal> deviceOnlineTotalWrapper = new LambdaQueryWrapper<>();
                count = deviceOnlineTotalMapper.selectCount(deviceOnlineTotalWrapper);
                log.info("****** DeviceOnlineTotal Count: {}",count);
                if (count <= deviceOnlineTotalMax){
                    return;
                }
                // 取得第一条的时间，再把当天的数据删除
                deviceOnlineTotalWrapper.clear();
                deviceOnlineTotalWrapper.orderByAsc(DeviceOnlineTotal::getId);
                DeviceOnlineTotal deviceOnlineTotal = deviceOnlineTotalService.getOne(deviceOnlineTotalWrapper,false);
                if (deviceOnlineTotal != null){
                    String s = DateUtils.format(deviceOnlineTotal.getStartTime(),"yyyy-MM-dd");
                    Date from = DateUtils.parse(s + " 00:00:00","yyyy-MM-dd HH:mm:ss");
                    from  = new Date((from.getTime() + 24 * 60 * 60 * 1000L));
                    deviceOnlineTotalWrapper.clear();
                    deviceOnlineTotalWrapper.lt(DeviceOnlineTotal::getStartTime,from);
                    deviceOnlineTotalService.remove(deviceOnlineTotalWrapper);
                }
            }
        }).start();
        //4、报警数据
        new Thread(()->{
            int count=deviceAlarmMax+1;
            //每个循环删除一天的数据，只要多于最大值，就一直删
            while(count > deviceAlarmMax){
                //总数
                LambdaQueryWrapper<DeviceAlarm> deviceAlarmWrapper = new LambdaQueryWrapper<>();
                count = deviceAlarmMapper.selectCount(deviceAlarmWrapper);
                log.info("****** DeviceAlarm Count: {}",count);
                if (count <= deviceAlarmMax){
                    return;
                }
                // 取得第一条的时间，再把当天的数据删除
                deviceAlarmWrapper.clear();
                deviceAlarmWrapper.orderByAsc(DeviceAlarm::getId);
                DeviceAlarm deviceAlarm = deviceAlarmService.getOne(deviceAlarmWrapper,false);
                if (deviceAlarm != null){
                    String s = DateUtils.format(deviceAlarm.getAlarmTime(),"yyyy-MM-dd");
                    Date from = DateUtils.parse(s + " 00:00:00","yyyy-MM-dd HH:mm:ss");
                    from  = new Date((from.getTime() + 24 * 60 * 60 * 1000L));
                    deviceAlarmWrapper.clear();
                    deviceAlarmWrapper.lt(DeviceAlarm::getAlarmTime,from);
                    deviceAlarmService.remove(deviceAlarmWrapper);
                }
            }
        }).start();

        //5、定位数据
        new Thread(()->{
            int count=devicePositionMax+1;
            //每个循环删除一天的数据，只要多于最大值，就一直删
            while(count > devicePositionMax){
                //总数
                LambdaQueryWrapper<DevicePosition> devicePositionWrapper = new LambdaQueryWrapper<>();
                count = devicePositionMapper.selectCount(devicePositionWrapper);
                log.info("****** DeviceAlarm Count: {}",count);
                if (count <= devicePositionMax){
                    return;
                }
                // 取得第一条的时间，再把当天的数据删除
                devicePositionWrapper.clear();
                devicePositionWrapper.orderByAsc(DevicePosition::getId);
                DevicePosition devicePosition = devicePositionService.getOne(devicePositionWrapper,false);
                if (devicePosition != null){
                    String s = DateUtils.format(devicePosition.getTime(),"yyyy-MM-dd");
                    Date from = DateUtils.parse(s + " 00:00:00","yyyy-MM-dd HH:mm:ss");
                    from  = new Date((from.getTime() + 24 * 60 * 60 * 1000L));
                    devicePositionWrapper.clear();
                    devicePositionWrapper.lt(DevicePosition::getTime,from);
                    devicePositionService.remove(devicePositionWrapper);
                }
            }
        }).start();

    }
}
